package org.hank.demoproject.utils

import android.util.ArrayMap
import android.view.View
import androidx.annotation.MainThread
import androidx.fragment.app.Fragment
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import com.bumptech.glide.Glide
import com.bumptech.glide.RequestManager
import com.bumptech.glide.manager.LifecycleListener
import com.bumptech.glide.manager.RequestManagerTreeNode
import com.bumptech.glide.util.Util
import java.util.*
import com.bumptech.glide.manager.Lifecycle as GlideLifecycle

//  ┏┓　　　┏┓
//┏┛┻━━━┛┻┓
//┃　　　　　　　┃
//┃　　　━　　　┃
//┃　┳┛　┗┳　┃
//┃　　　　　　　┃
//┃　　　┻　　　┃
//┃　　　　　　　┃
//┗━┓　　　┏━┛
//    ┃　　　┃                  神兽保佑
//    ┃　　　┃                  永无BUG！
//    ┃　　　┗━━━┓
//    ┃　　　　　　　┣┓
//    ┃　　　　　　　┏┛
//    ┗┓┓┏━┳┓┏┛
//      ┃┫┫　┃┫┫
//      ┗┻┛　┗┻┛
/**
 *@auth: Hank
 *邮箱: cs16xiaoc1@163.com
 *创建时间: 2021/9/23 10:47
 *描述: 使用Navigation情况下 glide生命周期处理
 */
class KGlide {

    companion object {
        private val lifecycleMap = ArrayMap<LifecycleOwner, RequestManager>()

        @MainThread
        fun with(fragment: Fragment): RequestManager {
            Util.assertMainThread()

            val lifecycleOwner = fragment.viewLifecycleOwner
            if (lifecycleOwner.lifecycle.currentState == Lifecycle.State.DESTROYED) {
                throw IllegalStateException("View is already destroyed")
            }

            if (lifecycleMap[lifecycleOwner] == null) {
                val appContext = fragment.requireContext().applicationContext
                lifecycleMap[lifecycleOwner] = RequestManager(
                    Glide.get(appContext),
                    KLifecycle(lifecycleOwner.lifecycle),
                    KEmptyRequestManagerTreeNode(), appContext
                )
            }
            return lifecycleMap[lifecycleOwner]!!
        }
    }

    class KEmptyRequestManagerTreeNode : RequestManagerTreeNode {
        override fun getDescendants(): Set<RequestManager> {
            return emptySet()
        }

    }

    class KLifecycle(private val lifecycle: Lifecycle) : GlideLifecycle {
        private val lifecycleListeners =
            Collections.newSetFromMap(WeakHashMap<LifecycleListener, Boolean>())

        private val lifecycleObserver =
            object : DefaultLifecycleObserver {
                override fun onStart(owner: LifecycleOwner) {
                    val listeners = Util.getSnapshot(lifecycleListeners)
                    for (listener in listeners) {
                        listener.onStart()
                    }
                }

                override fun onStop(owner: LifecycleOwner) {
                    val listeners = Util.getSnapshot(lifecycleListeners)
                    for (listener in listeners) {
                        listener.onStop()
                    }
                }

                override fun onDestroy(owner: LifecycleOwner) {
                    val listeners = Util.getSnapshot(lifecycleListeners)
                    for (listener in listeners) {
                        listener.onDestroy()
                    }

                    lifecycleMap.remove(owner)
                    lifecycleListeners.clear()
                    lifecycle.removeObserver(this)
                }
            }

        init {
            lifecycle.addObserver(lifecycleObserver)
        }

        override fun addListener(listener: LifecycleListener) {
            lifecycleListeners.add(listener)
            when (lifecycle.currentState) {
                Lifecycle.State.STARTED, Lifecycle.State.RESUMED -> listener.onStart()
                Lifecycle.State.DESTROYED -> listener.onDestroy()
                else ->
                    listener.onStop()
            }
        }

        override fun removeListener(listener: LifecycleListener) {
            lifecycleListeners.remove(listener)
        }
    }
}